<template>
  <div style="min-width: 540px;width:600px;">
    <div class="eleme">
      <el-upload
        ref="upload"
        action="#"
        :before-upload="beforeUpload"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :on-change="handleChange"
        :auto-upload="false"
        :show-file-list="false"
      >
        <el-button slot="trigger" size="small" type="primary"
          >选取图片</el-button
        >
        <el-button
          style="margin-left: 10px;"
          size="small"
          type="success"
          @click="submitUpload"
          >上传头像</el-button
        >
        <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
      </el-upload>
    </div>
    <div>
      <br />
      <el-button
        type="primary"
        icon="el-icon-refresh-right"
        circle
        @click="rotateRight"
      ></el-button>
      <el-button
        type="success"
        icon="el-icon-refresh-left"
        circle
        @click="rotateLeft"
      ></el-button>
      <el-button
        type="danger"
        icon="el-icon-plus"
        circle
        @click="changeScale(1)"
      ></el-button>
      <el-button
        type="warning"
        icon="el-icon-minus"
        circle
        @click="changeScale(-1)"
      ></el-button>
    </div>
    <div class="cropper">
      <div class="cropper-content" style="margin-top:60px;margin-left:60px;">
        <div class="cropper">
          <vueCropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.size"
            :outputType="option.outputType"
            :info="true"
            :full="option.full"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :original="option.original"
            :autoCrop="option.autoCrop"
            :autoCropWidth="option.autoCropWidth"
            :autoCropHeight="option.autoCropHeight"
            :fixedBox="option.fixedBox"
            @realTime="realTime"
            @imgLoad="imgLoad"
            :centerBox="true"
          ></vueCropper>
        </div>
        <div style="margin-left:40px;">
          <div
            class="show-preview"
            :style="{
              width: '155px',
              height: '155px',
              overflow: 'hidden',
              margin: '5px'
            }"
          >
            <div :style="previews.div" class="preview">
              <img :src="previews.url" :style="previews.img" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { VueCropper } from 'vue-cropper'
import axios from 'axios'
export default {
  components: {
    VueCropper
  },
  data() {
    return {
      headImg: '',
      //剪切图片上传
      crap: false,
      previews: {},
      option: {
        img: '',
        outputSize: 1, //剪切后的图片质量（0.1-1）
        full: false, //输出原图比例截图 props名full
        outputType: 'png',
        canMove: true,
        original: false,
        canMoveBox: true,
        autoCrop: true,
        autoCropWidth: 150,
        autoCropHeight: 150,
        fixedBox: false
      },
      fileName: '', //本机文件地址
      downImg: '#',
      imgFile: '',
      uploadImgRelaPath: '' //上传后的图片的地址（不带服务器域名）
    }
  },
  methods: {
    handleChange(file, fileList) {
      console.log('上传文件')
      console.log(file)
      let data = window.URL.createObjectURL(new Blob([file.raw]))
      this.fileName = file.name
      this.option.img = data
    },
    submitUpload(file) {
      // this.$refs.upload.submit();
      this.finish('blob')
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
      // let data = window.URL.createObjectURL(new Blob([file]));
      // this.option.img = data;
    },
    beforeUpload(file) {
      // console.log('上传文件')
      // console.log(file)
      // let data = window.URL.createObjectURL(new Blob([file]))
      // this.fileName = file.name
      // this.option.img = data
    },
    //放大/缩小
    changeScale(num) {
      console.log('changeScale')
      num = num || 1
      this.$refs.cropper.changeScale(num)
    },
    //坐旋转
    rotateLeft() {
      console.log('rotateLeft')
      this.$refs.cropper.rotateLeft()
    },
    //右旋转
    rotateRight() {
      console.log('rotateRight')
      this.$refs.cropper.rotateRight()
    },
    //上传图片（点击上传按钮）
    finish(type) {
      console.log('finish')
      let _this = this
      let formData = new FormData()
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data', //之前说的以表单传数据的格式来传递fromdata
          'User-Token': this.$store.state.token
        }
      }
      const instance = axios.create({
        withCredentials: false
      })
      // 输出
      if (type === 'blob') {
        this.$refs.cropper.getCropBlob(data => {
          // let img = window.URL.createObjectURL(data)
          // this.model = true
          // this.modelSrc = img
          // 此处的file为后台接收的文件输入框的name我自己的后台接收的是headerImg
          formData.append('headerImg', data, this.fileName)
          formData.append('uid', this.$store.state.uid)
          console.log(formData)
          instance.post('uploadHeaderImg.calc', formData, config).then(res => {
            if (res.data.code !== '200') return this.$message.error('上传失败')
            this.$message.success('上传成功')
            // this.$store.dispatch('getImg', this.$store.state.uid)
            // this.$refs.upload.clearFiles()
            // this.option = {
            //   img: '',
            //   outputSize: 1, //剪切后的图片质量（0.1-1）
            //   full: false, //输出原图比例截图 props名full
            //   outputType: 'png',
            //   canMove: true,
            //   original: false,
            //   canMoveBox: true,
            //   autoCrop: true,
            //   autoCropWidth: 150,
            //   autoCropHeight: 150,
            //   fixedBox: false
            // }
            this.$router.go(0)
          })
        })
      } else {
        this.$refs.cropper.getCropData(data => {
          this.model = true
          this.modelSrc = data
        })
      }
    },
    // 实时预览函数
    realTime(data) {
      console.log('realTime')
      this.previews = data
    },
    imgLoad(msg) {
      console.log('imgLoad')
      console.log(msg)
    }
  }
}
</script>

<style lang="less" scoped>
.cropper-content {
  min-width: 540px;
  display: flex;
  //   display: -webkit-flex;
  //   justify-content: flex-end;
  //   -webkit-justify-content: flex-end;
  .cropper {
    width: 260px;
    height: 260px;
  }
  .show-preview {
    flex: 1;
    -webkit-flex: 1;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    -webkit-justify-content: center;
    .preview {
      overflow: hidden;
      border-radius: 50%;
      border: 1px solid #cccccc;
      background: #cccccc;
      margin-left: 40px;
    }
  }
}
.cropper-content .show-preview .preview {
  margin-left: 0;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  -khtml-user-select: none;
  user-select: none;
}
</style>
